package lgvalle.com.fluxtodo.flux.fluxbase;

import android.util.Log;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

/**
 * Created by lgvalle on 22/07/15.
 */
public class Action {
    // action 的type
    private final String type;
    // action 的数据
    private final Map<String, Object> dataMap;
    //private final Map<String, Class> dataClass;

    // 依赖注入
    public Action(String type, Map<String, Object> dataMap/*, Map<String, Class> dataClass*/) {
        this.type = type;
        this.dataMap = dataMap;
        //this.dataClass = dataClass;
    }

    public static Builder type(String type) {
        return new Builder().with(type);
    }

    // 获取类型 数据
    public String getType() {
        return type;
    }

    public Map getDataMap() {
        return dataMap;
    }

    public Object getData(String key) {
        if (dataMap != null && dataMap.size() > 0 && dataMap.containsKey(key)) {
            return dataMap.get(key);
        }
        return null;
    }
    // action 构造器
    public static class Builder {

        private String type;
        private Map<String, Object> data;
        //private Map<String, Class> dataClass;

        // 构造器注入type 和空map
        Builder with(String type) {
            if (type == null) {
                throw new IllegalArgumentException("Type may not be null.");
            }
            this.type = type;
            this.data = new HashMap<>();
            //this.dataClass = new Hashtable<>();
            return this;
        }

        // 设置数据
        public Builder bundle(String key, Object value) {
            if (key == null) {
                throw new IllegalArgumentException("Key may not be null.");
            }

            if (value == null) {
                throw new IllegalArgumentException("Value may not be null.");
            }
            Log.d("bruce", "value: " + value + " class" + value.getClass().getSimpleName());
            data.put(key, value);
            //dataClass.put(key, value.getClass());
            return this;
        }

        // 构造action
        public Action build() {
            if (type == null || type.isEmpty()) {
                throw new IllegalArgumentException("At least one key is required.");
            }
            return new Action(type, data/*, dataClass*/);
        }
    }
}
